package com.yc.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yc.R_Code;
import com.yc.Result;
import com.yc.entities.BloodSugarQualityControl;
import com.yc.service.BloodSugarQualityControlService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 质控批号表 前端控制器
 * </p>
 *
 * @author kuakha
 * @since 2021-04-20
 */
@RestController
@RequestMapping("/bloodSugarQualityControl")
@Api(value = "BloodSugarArticleTryController", tags = {"质控批号"})
public class BloodSugarQualityControlController {

    @Autowired
    private BloodSugarQualityControlService bloodSugarQualityControlService;

    @ApiOperation(value = "pc添加质控批号信息")
    @PreAuthorize("hasAnyAuthority('sys:bedManage','sys:noAccess')")
    @PostMapping("addQualityControl")
    @Transactional(rollbackFor = Exception.class)
    public Result addQualityControl(@Valid @RequestBody BloodSugarQualityControl bloodSugarQualityControl) {
        LambdaQueryWrapper<BloodSugarQualityControl> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(BloodSugarQualityControl::getQualityControlName, bloodSugarQualityControl.getQualityControlName());
        if (bloodSugarQualityControlService.count(wrapper) > 0) {
            return Result.error().message("已经存在该质控批号");
        }
        bloodSugarQualityControl.setAddTime(new Date());
        boolean save = bloodSugarQualityControlService.save(bloodSugarQualityControl);
        if (save) {
            return Result.ok();
        } else {
            return Result.error().code(R_Code.R_ADD_FAIL);
        }
    }

    @ApiOperation(value = "pc查询所有质控批号信息")
    @PreAuthorize("hasAnyAuthority('sys:roleManage','sys:noAccess')")
    @GetMapping("getQualityControl")
    public Result getQualityControl(@ApiParam(name = "deanId", value = "院长id") @RequestParam Integer deanId,
                                    @ApiParam(name = "departmentId", value = "科室id") @RequestParam(required = false) Integer departmentId,
                                    @ApiParam(name = "current", value = "当前页码") @RequestParam(required = false) Integer current,
                                    @ApiParam(name = "size", value = "一页显示多少条") @RequestParam(required = false) Integer size) {
        return bloodSugarQualityControlService.getQualityControl(deanId, departmentId, current, size);
    }

    @ApiOperation(value = "pc查询所有质控批号信息 下拉框使用")
    @PreAuthorize("hasAnyAuthority('sys:roleManage','sys:noAccess')")
    @GetMapping("getAllQualityControl")
    public Result getAllQualityControl(@ApiParam(name = "deanId", value = "院长id") @RequestParam Integer deanId,
                                       @ApiParam(name = "departmentId", value = "科室id") @RequestParam Integer departmentId) {
        LambdaQueryWrapper<BloodSugarQualityControl> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(BloodSugarQualityControl::getDeanId, deanId).eq(BloodSugarQualityControl::getDepartmentId, departmentId);
        List<BloodSugarQualityControl> list = bloodSugarQualityControlService.list(wrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            return Result.ok().data(list);
        } else {
            return Result.error().code(R_Code.R_NULL_DATA_FAIL);
        }
    }

    @ApiOperation(value = "APP通过院长id和科室Id查看质控批号")
    @PostMapping("getAllQualityByDeanIdAndDepartmentId")
    public Result getAllQualityByDeanIdAndDepartmentId(String departmentId, String deanId) {
        if (StringUtils.isEmpty(departmentId)) {
            return Result.error().code(301).message("科室Id不能为空");
        }
        if (StringUtils.isEmpty(deanId)) {
            return Result.error().code(302).message("院长Id不能为空");
        }

        QueryWrapper<BloodSugarQualityControl> query = Wrappers.query();
        query.eq("department_id", departmentId);
        query.eq("dean_id", deanId);
        List<BloodSugarQualityControl> list = bloodSugarQualityControlService.list(query);
        if (CollectionUtils.isNotEmpty(list)) {
            return Result.ok().data(list);
        } else {
            return Result.error().code(300).message("暂无数据");
        }
    }

    @ApiOperation(value = "pc根据id查询质控批号信息")
    @PreAuthorize("hasAnyAuthority('sys:roleManage','sys:noAccess')")
    @GetMapping("getQualityControlById")
    public Result getQualityControlById(@ApiParam(name = "qualityId", value = "质控批号id") @RequestParam Integer qualityId) {
        BloodSugarQualityControl byId = bloodSugarQualityControlService.getById(qualityId);
        if (byId != null) {
            return Result.ok().data(byId);
        } else {
            return Result.error().code(R_Code.R_NULL_DATA_FAIL);
        }
    }

    @ApiOperation(value = "pc删除质控批号信息")
    @PreAuthorize("hasAnyAuthority('sys:roleManage','sys:noAccess')")
    @GetMapping("deleteQualityControl")
    public Result deleteQualityControl(@ApiParam(name = "qualityId", value = "质控批号id") @RequestParam Integer qualityId) {
        boolean b = bloodSugarQualityControlService.removeById(qualityId);
        if (b) {
            return Result.ok();
        } else {
            return Result.error().code(R_Code.R_DELETE_FAIL);
        }
    }

    @ApiOperation(value = "pc修改质控批号信息")
    @PreAuthorize("hasAnyAuthority('sys:bedManage','sys:noAccess')")
    @PostMapping("updateQualityControl")
    @Transactional(rollbackFor = Exception.class)
    public Result updateQualityControl(@Valid @RequestBody BloodSugarQualityControl bloodSugarQualityControl) {
        LambdaQueryWrapper<BloodSugarQualityControl> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(BloodSugarQualityControl::getQualityControlName, bloodSugarQualityControl.getQualityControlName())
            .ne(BloodSugarQualityControl::getId,bloodSugarQualityControl.getId());
        if (bloodSugarQualityControlService.count(wrapper) > 0) {
            return Result.error().message("已经存在该质控批号");
        }
        bloodSugarQualityControl.setAddTime(new Date());
        boolean updateById = bloodSugarQualityControlService.updateById(bloodSugarQualityControl);
        if (updateById) {
            return Result.ok();
        } else {
            return Result.error().code(R_Code.R_UPDATE_FAIL);
        }
    }
}

